/**
 * Created by xiangyufang on 2016/9/22.
 */
;(function($,window,document,undefined){
    /***详情页面
     * 后台传递一个ID和TYPE值
     * 以此来判定是那个设备类型下的那一条数据
     * ***/
    //从后台传过来的全局数据ID和Type
    var comId = $('#JQ_common_id').val();
    var type = $('#JQ_common_type').val();

    //标题部分
    var $title_val = $('#JQ_title_content');

    //表单部分
    var $power_form = $('#JQ_power');
    var $long_form = $('#JQ_long');
    var $bed_form = $('#JQ_bed');
    var $insu_form = $('#JQ_insu');
    var $avoid_form = $('#JQ_avoid');
    var $line_form = $('#JQ_line');
    var $pile_form = $('#JQ_pile');
    var $sacri_form = $('#JQ_sacri');
    var $elec_form = $('#JQ_elec');

    //数据筛选
    var filter = {
        //阴保电源数据表单
        getPowerFilter: function(data){
            var filter = {};
            filter.mode = true;
            filter.eqptId = comId;
            // 分页参数
            if(data){
                if (data.start == 0) {
                    filter['jsonBean.pageNum'] = 1;
                } else {
                    filter['jsonBean.pageNum'] = data.start / 10 + 1;
                }
                filter['jsonBean.pageCount'] = data.length;
            }
            // 购买时间1
            filter.rectificationDateBegin = function(){
                var v  = $('#JQ_power_startDate').val();
                if(v){
                    return v;
                }
                return '';
            }();
            // 购买时间2
            filter.rectificationDateEnd = function(){
                var v1  = $('#JQ_power_startDate').val();
                var v2  = $('#JQ_power_endDate').val();
                if(v2){
                    if(v1){
                        if(!(Dyy.checkDateLate(v1,v2))){
                            $('#JQ_power_endDate').TIP({content:'结束日期应晚于开始日期!'});
                            filter.mode = false;
                        }
                    }
                    return v2;
                }
                return '';
            }();
            return filter;
        },
        //长效参比数据表单
        getLongFilter: function(data){
            var filter = {};
            filter.mode = true;
            filter.eqptId = comId;
            // 分页参数
            if(data){
                if (data.start == 0) {
                    filter['jsonBean.pageNum'] = 1;
                } else {
                    filter['jsonBean.pageNum'] = data.start / 10 + 1;
                }
                filter['jsonBean.pageCount'] = data.length;
            }
            // 购买时间1
            filter.rectificationDateBegin = function(){
                var v  = $('#JQ_long_startDate').val();
                if(v){
                    return v;
                }
                return '';
            }();
            // 购买时间2
            filter.rectificationDateEnd = function(){
                var v1  = $('#JQ_long_startDate').val();
                var v2  = $('#JQ_long_endDate').val();
                if(v2){
                    if(v1){
                        if(!(Dyy.checkDateLate(v1,v2))){
                            $('#JQ_long_endDate').TIP({content:'结束日期应晚于开始日期!'});
                            filter.mode = false;
                        }
                    }
                    return v2;
                }
                return '';
            }();
            return filter;
        },
        //阳极地床数据表单
        getBedFilter: function(data){
            var filter = {};
            filter.mode = true;
            filter.eqptId = comId;
            // 分页参数
            if(data){
                if (data.start == 0) {
                    filter['jsonBean.pageNum'] = 1;
                } else {
                    filter['jsonBean.pageNum'] = data.start / 10 + 1;
                }
                filter['jsonBean.pageCount'] = data.length;
            }
            // 购买时间1
            filter.rectificationDateBegin = function(){
                var v  = $('#JQ_bed_startDate').val();
                if(v){
                    return v;
                }
                return '';
            }();
            // 购买时间2
            filter.rectificationDateEnd = function(){
                var v1  = $('#JQ_bed_startDate').val();
                var v2  = $('#JQ_bed_endDate').val();
                if(v2){
                    if(v1){
                        if(!(Dyy.checkDateLate(v1,v2))){
                            $('#JQ_bed_endDate').TIP({content:'结束日期应晚于开始日期!'});
                            filter.mode = false;
                        }
                    }
                    return v2;
                }
                return '';
            }();
            return filter;
        },
        //绝缘装置数据表单
        getInsuFilter: function(data){
            var filter = {};
            filter.mode = true;
            filter.eqptId = comId;
            // 分页参数
            if(data){
                if (data.start == 0) {
                    filter['jsonBean.pageNum'] = 1;
                } else {
                    filter['jsonBean.pageNum'] = data.start / 10 + 1;
                }
                filter['jsonBean.pageCount'] = data.length;
            }
            // 购买时间1
            filter.rectificationDateBegin = function(){
                var v  = $('#JQ_insu_startDate').val();
                if(v){
                    return v;
                }
                return '';
            }();
            // 购买时间2
            filter.rectificationDateEnd = function(){
                var v1  = $('#JQ_insu_startDate').val();
                var v2  = $('#JQ_insu_endDate').val();
                if(v2){
                    if(v1){
                        if(!(Dyy.checkDateLate(v1,v2))){
                            $('#JQ_insu_endDate').TIP({content:'结束日期应晚于开始日期!'});
                            filter.mode = false;
                        }
                    }
                    return v2;
                }
                return '';
            }();
            return filter;
        },
        //防浪涌数据表单
        getAvoidFilter: function(data){
            var filter = {};
            filter.mode = true;
            filter.eqptId = comId;
            // 分页参数
            if(data){
                if (data.start == 0) {
                    filter['jsonBean.pageNum'] = 1;
                } else {
                    filter['jsonBean.pageNum'] = data.start / 10 + 1;
                }
                filter['jsonBean.pageCount'] = data.length;
            }
            // 购买时间1
            filter.rectificationDateBegin = function(){
                var v  = $('#JQ_avoid_startDate').val();
                if(v){
                    return v;
                }
                return '';
            }();
            // 购买时间2
            filter.rectificationDateEnd = function(){
                var v1  = $('#JQ_avoid_startDate').val();
                var v2  = $('#JQ_avoid_endDate').val();
                if(v2){
                    if(v1){
                        if(!(Dyy.checkDateLate(v1,v2))){
                            $('#JQ_avoid_endDate').TIP({content:'结束日期应晚于开始日期!'});
                            filter.mode = false;
                        }
                    }
                    return v2;
                }
                return '';
            }();
            return filter;
        },
        //跨接线数据表单
        getLineFilter: function(data){
            var filter = {};
            filter.mode = true;
            filter.eqptId = comId;
            // 分页参数
            if(data){
                if (data.start == 0) {
                    filter['jsonBean.pageNum'] = 1;
                } else {
                    filter['jsonBean.pageNum'] = data.start / 10 + 1;
                }
                filter['jsonBean.pageCount'] = data.length;
            }
            // 购买时间1
            filter.rectificationDateBegin = function(){
                var v  = $('#JQ_line_startDate').val();
                if(v){
                    return v;
                }
                return '';
            }();
            // 购买时间2
            filter.rectificationDateEnd = function(){
                var v1  = $('#JQ_line_startDate').val();
                var v2  = $('#JQ_line_endDate').val();;
                if(v2){
                    if(v1){
                        if(!(Dyy.checkDateLate(v1,v2))){
                            $('#JQ_line_endDate').TIP({content:'结束日期应晚于开始日期!'});
                            filter.mode = false;
                        }
                    }
                    return v2;
                }
                return '';
            }();
            return filter;
        },
        //阴保测试桩数据表单
        getPileFilter: function(data){
            var filter = {};
            filter.mode = true;
            filter.eqptId = comId;
            // 分页参数
            if(data){
                if (data.start == 0) {
                    filter['jsonBean.pageNum'] = 1;
                } else {
                    filter['jsonBean.pageNum'] = data.start / 10 + 1;
                }
                filter['jsonBean.pageCount'] = data.length;
            }
            // 购买时间1
            filter.rectificationDateBegin = function(){
                var v  = $('#JQ_pile_startDate').val();
                if(v){
                    return v;
                }
                return '';
            }();
            // 购买时间2
            filter.rectificationDateEnd = function(){
                var v1  = $('#JQ_pile_startDate').val();
                var v2  = $('#JQ_pile_endDate').val();
                if(v2){
                    if(v1){
                        if(!(Dyy.checkDateLate(v1,v2))){
                            $('#JQ_pile_endDate').TIP({content:'结束日期应晚于开始日期!'});
                            filter.mode = false;
                        }
                    }
                    return v2;
                }
                return '';
            }();
            return filter;
        },
        //牺牲阳极数据表单
        getSacriFilter: function(data){
            var filter = {};
            filter.mode = true;
            filter.eqptId = comId;
            // 分页参数
            if(data){
                if (data.start == 0) {
                    filter['jsonBean.pageNum'] = 1;
                } else {
                    filter['jsonBean.pageNum'] = data.start / 10 + 1;
                }
                filter['jsonBean.pageCount'] = data.length;
            }
            // 购买时间1
            filter.rectificationDateBegin = function(){
                var v  = $('#JQ_sacri_startDate').val();
                if(v){
                    return v;
                }
                return '';
            }();
            // 购买时间2
            filter.rectificationDateEnd = function(){
                var v1  = $('#JQ_sacri_startDate').val();
                var v2  = $('#JQ_sacri_endDate').val();
                if(v2){
                    if(v1){
                        if(!(Dyy.checkDateLate(v1,v2))){
                            $('#JQ_sacri_endDate').TIP({content:'结束日期应晚于开始日期!'});
                            filter.mode = false;
                        }
                    }
                    return v2;
                }
                return '';
            }();
            return filter;
        },
        //排流装置数据表单
        getElecFilter: function(data){
            var filter = {};
            filter.mode = true;
            filter.eqptId = comId;
            // 分页参数
            if(data){
                if (data.start == 0) {
                    filter['jsonBean.pageNum'] = 1;
                } else {
                    filter['jsonBean.pageNum'] = data.start / 10 + 1;
                }
                filter['jsonBean.pageCount'] = data.length;
            }
            // 购买时间1
            filter.rectificationDateBegin = function(){
                var v  = $('#JQ_elec_startDate').val();
                if(v){
                    return v;
                }
                return '';
            }();
            // 购买时间2
            filter.rectificationDateEnd = function(){
                var v1  = $('#JQ_elec_startDate').val();
                var v2  = $('#JQ_elec_endDate').val();
                if(v2){
                    if(v1){
                        if(!(Dyy.checkDateLate(v1,v2))){
                            $('#JQ_elec_endDate').TIP({content:'结束日期应晚于开始日期!'});
                            filter.mode = false;
                        }
                    }
                    return v2;
                }
                return '';
            }();
            return filter;
        }
    };
    if(type == 'CPPOWER'){
        //页面总体显示部分
        $power_form.show();
        $title_val.text('阴保电源');

        //对应的表单数据显示部分
        DyyBase.ajax(corrctrlApi.url_power_detail(), {eqptId:comId}, function (dataset) {
            $('#JQ_power_number').val(dataset.result.customNumber);
            $('#JQ_power_station').val(dataset.result.pipeValveroomName);
        });

    }else if(type == 'REFERENCE'){
        $long_form.show();
        $title_val.text('长效参比');

        //对应的表单数据显示部分
        DyyBase.ajax(corrctrlApi.url_long_detail(), {eqptId:comId}, function (dataset) {
            $('#JQ_long_pipe').val(dataset.result.pipeLineName);
            $('#JQ_long_segment').val(dataset.result.customNumber);
            $('#JQ_long_test').val(dataset.result.testStationNumber);
            $('#JQ_long_number').val(dataset.result.customNumber);
        });

    }else if(type == 'ANODESBED'){
        $bed_form.show();
        $title_val.text('阳极地床');

        //对应的表单数据显示部分
        DyyBase.ajax(corrctrlApi.url_bed_detail(), {eqptId:comId}, function (dataset) {
            $('#JQ_bed_number').val(dataset.result.customNumber);
            $('#JQ_bed_station').val(dataset.result.pipeValveroomName);
        });

    }else if(type == 'INSULATEDJOINT'){
        $insu_form.show();
        $title_val.text('绝缘装置');

        //对应的表单数据显示部分
        DyyBase.ajax(corrctrlApi.url_insu_detail(), {eqptId:comId}, function (dataset) {
            $('#JQ_insu_pipe').val(dataset.result.pipeLineName);
            $('#JQ_insu_station').val(dataset.result.pipeValveroomName);
            $('#JQ_insu_type').val(dataset.result.insulatedJointType);
            $('#JQ_insu_protect').val(dataset.result.protectionFacilities);
            $('#JQ_insu_test').val(dataset.result.testStationNumber);
            $('#JQ_insu_number').val(dataset.result.customNumber);
        });
    }else if(type == 'ANTISURGE'){
        $avoid_form.show();
        $title_val.text('防浪涌保护器');

        //对应的表单数据显示部分
        DyyBase.ajax(corrctrlApi.url_avoid_detail(), {eqptId:comId}, function (dataset) {
            $('#JQ_avoid_pipe').val(dataset.result.pipeLineName);
            $('#JQ_avoid_station').val(dataset.result.pipeValveroomName);
            $('#JQ_avoid_test').val(dataset.result.teststationnumber);
            $('#JQ_avoid_type').val(dataset.result.potectorType);
            $('#JQ_avoid_number').val(dataset.result.customNumber);
        });

    }else if(type == 'CPJUMPER'){
        $line_form.show();
        $title_val.text('');
        $title_val.text('跨接线');

        //对应的表单数据显示部分
        DyyBase.ajax(corrctrlApi.url_line_detail(), {eqptId:comId}, function (dataset) {
            $('#JQ_line_pipe').val(dataset.result.pipeLineName);
            $('#JQ_line_station').val(dataset.result.pipeValveroomName);
            $('#JQ_line_test').val(dataset.result.teststationnumber);
            $('#JQ_line_type').val(dataset.result.jumperType);
            $('#JQ_line_number').val(dataset.result.customNumber);
        });

    }else if(type == 'CPTESTSTATION'){
        $pile_form.show();
        $title_val.text('阴保测试桩');

        //对应的表单数据显示部分
        DyyBase.ajax(corrctrlApi.url_pile_detail(), {eqptId:comId}, function (dataset) {
            $('#JQ_pile_pipe').val(dataset.result.pipeLineName);
            $('#JQ_pile_test').val(dataset.result.customNumber);
            $('#JQ_pile_type').val(dataset.result.solderJointsCorrosion);
            $('#JQ_pile_function').val(dataset.result.purpose);
        });

    }else if(type == 'SAALANODE'){
        $sacri_form.show();
        $title_val.text('牺牲阳极');

        //对应的表单数据显示部分
        DyyBase.ajax(corrctrlApi.url_sacri_detail(), {eqptId:comId}, function (dataset) {
            $('#JQ_sacri_pipe').val(dataset.result.pipeLineName);
            $('#JQ_sacri_type').val(dataset.result.sacrificialAnodeType);
            $('#JQ_sacri_protect').val(dataset.result.protectionObject);
            $('#JQ_sacri_avoid').val(dataset.result.coatingMaterial);
            $('#JQ_sacri_test').val(dataset.result.teststationnumber);
        });
    }else if(type == 'CURRENTDRAINAGE'){
        $elec_form.show();
        $title_val.text('排流装置');

        //对应的表单数据显示部分
        DyyBase.ajax(corrctrlApi.url_elec_detail(), {eqptId:comId}, function (dataset) {
            $('#JQ_elec_pipe').val(dataset.result.pipeLineName);
            $('#JQ_elec_type').val(dataset.result.currentdrainage);
            $('#JQ_elec_avoid').val(dataset.result.interferenceType);
            $('#JQ_elec_way').val(dataset.result.drainageMethod);
            $('#JQ_elec_test').val(dataset.result.teststationnumber);
            $('#JQ_elec_number').val(dataset.result.customNumber);
        });
    }

    //通用表格
    var COMMONTABLE;
    var $common_table = $('#JQ_common_table');
    //表格数据加载
    COMMONTABLE = $common_table.DataTable({
        'language': {
            'paginate': {
                'next': '下一页',
                'previous': '上一页'
            },
            'info': '本次检索共 _TOTAL_ 条，每页显示 10 条，共 _PAGES_ 页',
            'zeroRecords': DyyBase.table_empty(),
            'infoEmpty': ''
        },
        'autoWidth': false,
        'order': [],
        'serverSide': true,
        'pageLength': 10,
        'ordering': false,
        'processing': false,
        'searching': false,
        'lengthChange': false,
        'ajax': function (data, callback) {
            var param;
            if(type == 'CPPOWER'){
                param = filter.getPowerFilter(data);
            }else if(type == 'REFERENCE'){
                param = filter.getLongFilter(data);
            }else if(type == 'ANODESBED'){
                param = filter.getBedFilter(data);
            }else if(type == 'INSULATEDJOINT'){
                param = filter.getInsuFilter(data);
            }else if(type == 'ANTISURGE'){
                param = filter.getAvoidFilter(data);
            }else if(type == 'CPJUMPER'){
                param = filter.getLineFilter(data);
            }else if(type == 'CPTESTSTATION'){
                param = filter.getPileFilter(data);
            }else if(type == 'SAALANODE'){
                param = filter.getSacriFilter(data);
            }else if(type == 'CURRENTDRAINAGE'){
                param = filter.getElecFilter(data);
            }
            DyyBase.ajax(corrctrlApi.url_detail_table(), param, function (dataset) {
                var returnData = {};
                returnData.draw = data.draw;
                returnData.recordsTotal = dataset.result.total;
                returnData.recordsFiltered = dataset.result.total;
                returnData.data = DyyBase.clearNull(dataset.result.list);
                callback(returnData);
            });
        },
        'drawCallback': function () {
        },
        'columns': [
            {
                data: 'description',
                render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
            },
            {
                data: 'rectificationDescription',
                render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
            },
            {
                data: 'rectificationCompany',
                render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
            },
            {
                data: 'rectificationDate',
                render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
            },
            {
                data: 'remarks',
                render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
            },
            {
                data: 'id',
                render: function (data) {
                    return '<i data-id="' + data + '" class="zy-details-icon" title="展开详情"></i>';
                }
            }
        ]
    });

    //阴保表格数据查询
    $('button.JQ_table_search').click(function () {
        var param;
        if(type == 'CPPOWER'){
            param = filter.getPowerFilter();
        }else if(type == 'REFERENCE'){
            param = filter.getLongFilter();
        }else if(type == 'ANODESBED'){
            param = filter.getBedFilter();
        }else if(type == 'INSULATEDJOINT'){
            param = filter.getInsuFilter();
        }else if(type == 'ANTISURGE'){
            param = filter.getAvoidFilter();
        }else if(type == 'CPJUMPER'){
            param = filter.getLineFilter();
        }else if(type == 'CPTESTSTATION'){
            param = filter.getPileFilter();
        }else if(type == 'SAALANODE'){
            param = filter.getSacriFilter();
        }else if(type == 'CURRENTDRAINAGE'){
            param = filter.getElecFilter();
        }
        if(param.mode){
            COMMONTABLE.draw();
        }
    });


    var rectifyTemplate = {

        // 阴保电源
        detail: [
            {name: 'eqptNumber', title: '设备编号：'},
            {name: 'description', title: '异常情况：'},
            {name: 'rectificationDate', title: '整改时间：'},
            {name: 'rectificationDescription', title: '整改记录：'},
            {name: 'rectificationCompany', title: '整改单位：'},
            {name: 'remarks', title: '备注：'}
        ]
    };

    //展开详情
    $common_table.delegate('i.zy-details-icon','click',function(event){

        DyyBase.showDetails($(this), COMMONTABLE, $common_table, rectifyTemplate['detail']);
        /*// 当前对象
        var $this_i = $(this);
        // 获取对象数据
        var data = $this_i.attr('data-a');
        // 父级tbody
        var $parent_tbody = $this_i.parent().parent().parent('tbody');
        // 父级tr
        var $parent_tr = $this_i.parent().parent('tr');
        // 查找tr
        var $tr_a = $parent_tbody.find('tr[data-a="tr_a_' + data + '"]');
        // 查找div
        var $div_a = $tr_a.find('div[data-a="div_a_' + data + '"]');
        if($tr_a.length > 0){
            $common_table.find('tr.zy-dy-tr').hide();
            $common_table.find('i').removeClass('active');
            $this_i.addClass('active');
            // 直接显示
            $tr_a.show();
        }else{
            $common_table.find('tr.zy-dy-tr').hide();
            $common_table.find('i').removeClass('active');
            $this_i.addClass('active');

            var $temp_tr = $('<tr class="zy-dy-tr" data-a="tr_a_' + data + '"><td colspan="6"><div data-a="div_a_' + data + '" class="zy-details-box"></div></td></tr>');
            $parent_tr.after($temp_tr);
            $tr_a = $parent_tbody.find('tr[data-a="tr_a_' + data + '"]');
            $div_a = $tr_a.find('div[data-a="div_a_' + data + '"]');

            fillDetails($div_a, $tr_a, $this_i, COMMONTABLE.row($parent_tr).data());
            $tr_a.show();
        }*/
        event.stopPropagation();
    });
    /*//详情内容
    function fillDetails(inner,tr,icon,data){
        var content = '<div class="zy-dy-inner">';
        content += '<div class="zy-dy-head">';
        content += '<p><span>设备编号：</span><span>' + data.eqptNumber + '</span></p>';
        content += '<p><span>异常情况：</span><span>' + data. description + '</span></p>';
        content += '<p><span>整改时间：</span><span>' + data.rectificationDate + '</span></p>';
        content += '<p><span>整改记录：</span><span>' + data.rectificationDescription + '</span></p>';
        content += '<p><span>整改单位：</span><span>' + data.rectificationCompany + '</span></p>';
        content += '<div class="zy-details-mark"><span>备注：</span><span>' + data.remarks + '</span></div>';
        content += '</div>';
        content += '</div>';
        content += '<div class="zy-xyf-footer"><button type="button" class="zy-button zy-pick-up" data-id=""><i></i>收起</button></div>';
        content += '</div>';
        inner.html(content);
        // 收起按钮
        var $pull = inner.find('button.zy-button');
        $pull.click(function () {
            icon.removeClass('active');
            tr.hide();
        });
    }*/

})(jQuery,window,document);